using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Models.Repositories
{
	#region using
	using WebClient.Helpers;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
    using WebClient.CF_DAL_WS;
	#endregion
	public class ContactRepository
	{
		public List<ContactDTO> SelectAll()
		{
			try
			{
                return DALService.Instance.Contact_SelectAll().ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<ContactDTO>();
		}

		public ContactDTO SelectByID(int id)
		{
			try
			{
                return DALService.Instance.Contact_SelectByID(id.ToString());
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return null;
		}

		public List<ContactDTO> SelectPaging(int page, int pageSize)
		{
			try
			{
                var lstItem = DALService.Instance.Contact_SelectAll().ToList();
				if (pageSize > 0)
				{
					int startIndex, itemCount;
					startIndex = (page - 1) * pageSize;
					itemCount = pageSize;
					if (startIndex + itemCount > lstItem.Count)
						itemCount = lstItem.Count - startIndex;
                    lstItem = lstItem.GetRange(startIndex, itemCount);
				}
				return lstItem;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - SelectPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - SelectPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<ContactDTO>();
		}

		public bool Insert(ContactDTO obj)
		{
			try
			{
                return DALService.Instance.Contact_Insert(obj);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

		public bool Delete(int id)
		{
			try
			{
                return DALService.Instance.Contact_Delete(id.ToString());
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

		public bool Update(ContactDTO obj)
		{
			try
			{
                return DALService.Instance.Contact_Update(obj);
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			try
			{
                return DALService.Instance.Contact_SelectAll().Length;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return -1;
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.NumContactPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<ContactDTO> RetrieveByID(int id)
		{
			try
			{
                return DALService.Instance.Contact_RetrieveByID(id.ToString()).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("Contact - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("Contact - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<ContactDTO>();
		}

	}
}